package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.BrandPromotionSettleInfoMapper;
import com.tulele.vtool.model.BrandPromotionSettleInfo;
import com.tulele.vtool.service.IBrandPromotionSettleInfoService;
import com.tulele.vtool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class BrandPromotionSettleInfoServiceImpl implements IBrandPromotionSettleInfoService {

    @Autowired
    private BrandPromotionSettleInfoMapper brandPromotionSettleInfoMapper;

    @Override
    public int processUserPromotionSettleInfo(BrandPromotionSettleInfo brandPromotionSettleInfo, double userChargeAmount) {

        int commissionProportion = Optional.ofNullable(brandPromotionSettleInfo.getCommissionProportion()).orElse(0);

        if (commissionProportion > 0) {

            double commissionIncome = (userChargeAmount * commissionProportion) / 100;

            double cumulativeIncome = Optional.ofNullable(brandPromotionSettleInfo.getCumulativeIncome()).orElse(0d);

            BrandPromotionSettleInfo finalBrandPromotionSettleInfo = new BrandPromotionSettleInfo();
            finalBrandPromotionSettleInfo.setId(brandPromotionSettleInfo.getId());
            finalBrandPromotionSettleInfo.setCumulativeIncome(cumulativeIncome + commissionIncome);
            finalBrandPromotionSettleInfo.setModifyTime(DateUtil.getCurDate());

            brandPromotionSettleInfoMapper.updBrandPromotionSettleInfo(finalBrandPromotionSettleInfo);

        }

        return 0;
    }

    @Override
    public BrandPromotionSettleInfo getBrandPromotionSettleInfo(Integer brandId) {
        return brandPromotionSettleInfoMapper.queryBrandPromotionSettleInfo(brandId);
    }

    @Override
    public int modifyBrandPromotionSettleInfo(BrandPromotionSettleInfo brandPromotionSettleInfo) {
        return brandPromotionSettleInfoMapper.updBrandPromotionSettleInfo(brandPromotionSettleInfo);
    }

    @Override
    public BrandPromotionSettleInfo getPromotionSettleByUserId(String userId) {
        return this.brandPromotionSettleInfoMapper.getPromotionSettleByUserId(userId);
    }
}
